<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.back()" fixed />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image round class="avatar" :src="profileObj.photo" @click="imgClickFn"/>
          <!-- file 选择框 -->
            <input
               type="file"
               ref="iptFile"
               v-show="false"
               accept="image/*"
               @change="onFileChange"
               />
        </template>
      </van-cell>
      <van-cell title="名称" is-link :value="profileObj.name" />
      <!-- <van-cell title="性别" is-link :value="profileObj.gender === 1 ? '女' : (profileObj.gender ===0 ? '男' :'保密')"  /> -->
      <!-- 数据映射 -->

      <van-cell title="性别" is-link   :value="sexObj[profileObj.gender]"/>
      <van-cell title="生日" is-link   :value="profileObj.birthday"/>
    </van-cell-group>
  </div>
</template>

<script>
import { getProfileAPI, uploadHeadImgAPI } from '@/api'
export default {
  data () {
    return {
      profileObj: {}, // 用户信息对象
      sexObj: {
        1: '女',
        0: '男',
        '-1': '保密'
      }

    }
  },
  name: 'UserEdit',
  async created () {
    const res = await getProfileAPI()
    console.log(res)
    this.profileObj = res.data.data
  },
  methods: {
    // 图片 点击事件
    imgClickFn () {
      this.$refs.iptFile.click()
    },
    // 选择文件
    async onFileChange (ev) {
      const formData = new FormData()
      formData.append('photo', ev.target.files[0])
      const res = await uploadHeadImgAPI(formData)
      this.profileObj.photo = res.data.data.photo
    }
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
</style>
